<template>
  <div class="goods-details">
    <transition @before-enter="beforeEnter" @enter="enter" @after-enter="afterEnter">
      <div class="ball" v-show="ballFlag" ref="ball"></div>
    </transition>
    <!-- 商品轮播图 -->
    <div class="mui-card">
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <Carousel :carousel="carousel"></Carousel>
        </div>
      </div>
    </div>
    <!-- 商品购买 -->
    <div class="mui-card">
      <div class="mui-card-header">{{ goods.title }}</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner">
          <p class="price">
            市场价：
            <span class="old_price">￥{{ goods.market_price }}</span>
            销售价：
            <span class="now_price">￥{{ goods.sell_price }}</span>
          </p>
          <p>
            购买数量：
            <NumberBox :max="goods.stock_quantity" @getSelectedCount="getSelectedCount"></NumberBox>
            <span class="quantity">(剩余: {{ goods.stock_quantity }})</span>
          </p>
          <p>
            <mt-button type="primary" size="small">立即购买</mt-button>
            <mt-button type="danger" size="small" @click="addToCart">加入购物车</mt-button>
          </p>
        </div>
      </div>
    </div>
    <!-- 商品参数 -->
    <div class="mui-card">
      <div class="mui-card-header">商品参数</div>
      <div class="mui-card-content">
        <div class="mui-card-content-inner goods_description">
          <span v-for="(item, index) in goods.description" :key="index">
            {{ item }}
          </span>
        </div>
      </div>
      <div class="mui-card-footer">
        <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
        <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
      </div>
    </div>
  </div>
</template>
<script>
import { Toast } from "mint-ui";
import Carousel from "../common/Carousel.vue";
import NumberBox from "../common/GoodsDetailsNumberBox.vue";
export default {
  data() {
    return {
      id: this.$route.params.id,
      goods: {},
      carousel: [],
      selectedCount: 1,
      ballFlag: false // 控制小球是否显示
    };
  },
  methods: {
    getGoods() {
      this.$http.get("getgoods?id=" + this.id).then(result => {
        if (result.status === 200 && result.body.length === 1) {
          this.goods = result.body[0];
          this.carousel = [
            { img: this.goods.img_url },
            { img: this.goods.img_url },
            { img: this.goods.img_url }
          ];
        } else Toast("获取商品信息失败");
      });
    },
    goDesc(id) {
      this.$router.push({
        name: "goodsDesc",
        params: { id }
      });
    },
    goComment(id) {
      this.$router.push({
        name: "goodsComment",
        params: { id }
      });
    },
    addToCart() {
      // 添加到购物车
      this.ballFlag = !this.ballFlag;
      // 拼接一个要保存到 store 中 cart 数组里的商品信息对象
      var goodsInfo = {
        id: this.id,
        title: this.goods.title,
        count: this.selectedCount,
        price: this.goods.sell_price,
        stock_quantity: this.goods.stock_quantity,
        selected: true,
        thumbnail:
          "https://img10.360buyimg.com/cms/s80x80" +
          this.goods.img_url.substring(this.goods.img_url.indexOf("_jfs"))
      };
      // 将商品信息保存到 store 中的 cart 数组
      this.$store.commit("addToCart", goodsInfo);
    },
    beforeEnter(el) {
      el.style.transform = "translate(0, 0)";
    },
    enter(el, done) {
      el.offsetWidth;
      // 获取小球在页面中的位置
      const ballPosition = this.$refs.ball.getBoundingClientRect();
      // Vue 不推荐我们操作DOM，但在这种只是获取坐标的需求上，可以使用操作DOM的方式
      const badgePostition = document
        .getElementById("cart_badge")
        .getBoundingClientRect();
      // 计算小球要移动的距离
      const xDist = badgePostition.left - ballPosition.left;
      const yDist = badgePostition.top - ballPosition.top;

      el.style.transform = `translate(${xDist}px, ${yDist}px)`;
      el.style.transition = "all .6s cubic-bezier(.5,-0.3,1,.68)";
      done();
    },
    afterEnter(el) {
      this.ballFlag = !this.ballFlag;
    },
    getSelectedCount(count) {
      this.selectedCount = count;
    }
  },
  components: {
    Carousel,
    NumberBox
  },
  created() {
    this.getGoods();
  }
};
</script>
<style lang="scss" scoped>
.goods-details {
  background-color: #ccc;
  overflow: hidden;
  .price {
    .old_price {
      text-decoration: line-through;
      margin-right: 10px;
      color: #aaa;
    }
    .now_price {
      color: #f00;
      font-size: 15px;
      font-weight: bold;
    }
  }
  .quantity {
    margin-left: 1em;
  }
  .goods_description {
    font-size: 12px;
    color: #aaa;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
  }
  .mui-card-footer {
    display: block;
    button {
      margin: 15px 0;
    }
  }
  .ball {
    width: 15px;
    height: 15px;
    border-radius: 50%;
    background-color: #f00;
    position: absolute;
    z-index: 99999;
    top: 431px;
    left: 154px;
  }
}
</style>

